package com.sunny.ihealth.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.jaeger.library.StatusBarUtil;
import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;
import com.sunny.ihealth.data.PreferencesHelper;
import com.sunny.ihealth.utils.NetworkUtils;

public class VisitorActivity extends BaseActivity implements View.OnClickListener {

    private static final int REQUEST_CODE_LOGIN = 1;
    private static final int REQUEST_CODE_REGISTER = 2;

    private Button btnLogin, btnRegister, btnTry;

    //从主界面来，，游客注册
    private boolean fromMain = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visitor);

        Intent intent = getIntent();
        if (intent != null) {
            fromMain = intent.getBooleanExtra("fromMain", false);
        }
        initViews();
    }


    @Override
    public void initViews() {
        StatusBarUtil.setTranslucent(this, 50);

        btnLogin = (Button) findViewById(R.id.btn_visitor_login);
        btnRegister = (Button) findViewById(R.id.btn_visitor_register);
        btnTry = (Button) findViewById(R.id.btn_visitor_try);

        btnLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        btnTry.setOnClickListener(this);

        //从主页来，不显示试用
        if (fromMain) {
            btnTry.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_visitor_login:
                if (!NetworkUtils.isNetworkAvailable(VisitorActivity.this, true)) {
                    return;
                }
                intent = new Intent(VisitorActivity.this, LoginActivity.class);
                if (fromMain) {
                    intent.putExtra("fromMain", true);
                }
                startActivityForResult(intent, REQUEST_CODE_LOGIN);
                break;
            case R.id.btn_visitor_register:
                if (!NetworkUtils.isNetworkAvailable(VisitorActivity.this, true)) {
                return;
            }
                intent = new Intent(VisitorActivity.this, RegisterActivity.class);
                if (fromMain) {
                    intent.putExtra("fromMain", true);
                }
                startActivityForResult(intent, REQUEST_CODE_REGISTER);
                break;
            case R.id.btn_visitor_try:
                getPreferencesHelper().setMode(PreferencesHelper.MODE_VISITOR);
                intent = new Intent(VisitorActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_LOGIN:
                if (resultCode == Activity.RESULT_OK) {
                    finish();
                }
                break;
            case REQUEST_CODE_REGISTER:
                if (resultCode == Activity.RESULT_OK) {
                    finish();
                }
                break;
        }
    }
}
